package com.zhao.modleclass.activity;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.zhao.modleclass.Helper.FragmentHelper;
import com.zhao.modleclass.R;
import com.zhao.modleclass.fragment.HomePageFragment;
import com.zhao.modleclass.fragment.LiveFragment;
import com.zhao.modleclass.fragment.MyPageFragment;
import com.zhao.modleclass.fragment.SearchClassFragmentText;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class MainActivity extends AppCompatActivity {
    private Context mContext = this;
    private RadioGroup main_radioGroup;
    private FrameLayout main_frameLayout;
    //底部导航按钮
    private RadioButton[] mRadioButton;
    //底部导航数组
    private String[] mArrBottonTab = null;
    //底部导航图片资源
    private Drawable[] mArrDrawable = null;
    //碎片数组
    private List<Fragment> mList = new ArrayList<>();

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置状态栏的颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            //获取样式中的属性值
            TypedValue typedValue = new TypedValue();
            this.getTheme().resolveAttribute(Color.parseColor("#FF4081"), typedValue, true);
            int[] attribute = new int[]{Color.parseColor("#FF4081")};
            TypedArray array = this.obtainStyledAttributes(typedValue.resourceId, attribute);
            int color = array.getColor(0, Color.parseColor("#FF4081"));
            array.recycle();
            window.setStatusBarColor(color);
        }
        initView();
        initBottonTableData();
        initBottonTable();
        initJpush();
    }


    //极光推送
    private void initJpush() {
        //设置标签  设置多个
        Set<String> tags = new HashSet<>();
        tags.add("小金针菇");
        tags.add("小昭昭");
        tags.add("文胸");
        JPushInterface.setTags(getApplicationContext(), tags, new TagAliasCallback() {
            @Override
            public void gotResult(int code, String errorMsg, Set<String> set) {
                if (code == 0) {
                    Log.i("TAG", "标签设置成功");
                } else {
                    Log.i("TAG", "标签设置失败");
                }
            }
        });
    }

    private void initView() {
        main_frameLayout = (FrameLayout) findViewById(R.id.main_frameLayout);
        main_radioGroup = (RadioGroup) findViewById(R.id.main_radiogRoup);
    }

    private void initBottonTableData() {
        //初始化底部导航数组
        mArrBottonTab = getResources().getStringArray(R.array.BottonTable);
        //初始化底部导航图片
        TypedArray array = getResources().obtainTypedArray(R.array.IconBottonTable);
        //初始化底部导航图片资源数组
        mArrDrawable = new Drawable[array.length()];
        for (int i = 0; i < mArrBottonTab.length; i++) {
            mArrDrawable[i] = array.getDrawable(i);
        }
        HomePageFragment homePageFragment = new HomePageFragment();
        mList.add(homePageFragment);
        LiveFragment liveFragment = new LiveFragment();
        mList.add(liveFragment);
//        SearchClassFragment searchClassFragment = new SearchClassFragment();
        SearchClassFragmentText searchClassFragmentText=new SearchClassFragmentText();
        mList.add(searchClassFragmentText);
        MyPageFragment myPageFragment = new MyPageFragment();
        mList.add(myPageFragment);
        //设置首页碎片，如果不设置，打开软件，当点击时候才会生成碎片布局
        FragmentHelper.switchFragment(getSupportFragmentManager(), mList, 0, R.id.main_frameLayout, 0, 0);

    }

    private void initBottonTable() {
        //设置底部导航初始和点击后的颜色
        final int otherColor = getResources().getColor(R.color.yellow);
        final int selectColor = getResources().getColor(R.color.black);
        //获取屏幕宽度
        int widthPixels = getResources().getDisplayMetrics().widthPixels;
        //初始化按钮
        mRadioButton = new RadioButton[mArrBottonTab.length];
        for (int i = 0; i < mArrBottonTab.length; i++) {
            //动态生成按钮
            mRadioButton[i] = new RadioButton(mContext);
            //设置按钮底部文字
            mRadioButton[i].setText(mArrBottonTab[i]);
            //设置底部导航按钮颜色
            mRadioButton[i].setButtonDrawable(android.R.color.transparent);
            //让文字居中
            mRadioButton[i].setGravity(Gravity.CENTER);
            //取消点击单选按钮产生的背景
            mRadioButton[i].setBackgroundResource(android.R.color.transparent);
            //给按钮占一个位置放置图标，设置四个边界
            mArrDrawable[i].setBounds(0, 0, mArrDrawable[i].getIntrinsicWidth(), mArrDrawable[i].getIntrinsicHeight());
            //设置按钮上的图标
            mRadioButton[i].setCompoundDrawables(null, mArrDrawable[i], null, null);
            //设置默认文字颜色
            mRadioButton[i].setTextColor(selectColor);
            //设置图标默认颜色
            mArrDrawable[i].setColorFilter(selectColor, PorterDuff.Mode.SRC_IN);
            //设置按钮的宽度
            mRadioButton[i].setWidth(widthPixels / mArrBottonTab.length);
            //将生产的按钮加入到管理组
            main_radioGroup.addView(mRadioButton[i]);


        }
        //设置默认按钮文字的颜色
        mRadioButton[0].setTextColor(otherColor);
        //默认按钮图标颜色
        mArrDrawable[0].setColorFilter(otherColor, PorterDuff.Mode.SRC_IN);
        //设置监听
        main_radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for (int i = 0; i < mArrBottonTab.length; i++) {
                    //设置默认颜色
                    mRadioButton[i].setTextColor(selectColor);
                    mArrDrawable[i].setColorFilter(selectColor, PorterDuff.Mode.SRC_IN);
                    //设置点击底部导航后图片切换的颜色
                    if (mRadioButton[i].getId() == checkedId) {
                        mRadioButton[i].setTextColor(otherColor);
                        mArrDrawable[i].setColorFilter(otherColor, PorterDuff.Mode.SRC_IN);
                        //动态生成碎片
                        FragmentHelper.switchFragment(getSupportFragmentManager(), mList, i, R.id.main_frameLayout, 0, 0);
                    }
                }
            }
        });
    }
}
